// 对应键名设置cookie
function setCookie(key, value, day, path = "/") {
    var date = new Date();
    date.setDate(date.getDate() + day);

    if (day != undefined) { // 有参数 => 设置过期时间  => 指定时间后过期
        document.cookie = key + "=" + encodeURIComponent(value) + "; expires=" + date.toUTCString() + ";path=" + path;
    } else { //没有参数 => 不设置过期时间 => 浏览器关闭过期
        document.cookie = key + "=" + encodeURIComponent(value) + ";path=" + path;
    }
}

// 对应键名获取cookie
function getCookie(key) { // 传入的键名
    var cookie = document.cookie;
    if (cookie) {
        var list = cookie.split("; ");
        // console.log(list);
        for (var i = 0; i < list.length; i++) {
            var item = list[i];  // 'user=a123123', 'pwd=123123'
            var name = item.split("=")[0]; //解析的键名
            var val = item.split("=")[1];
            if (key === name) {
                return decodeURIComponent(val);
            }
        }

    }
    return ""; // 找不到 默认返回 ""
}